package com.pany.rememberpass;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class LoginActivity extends AppCompatActivity {
    private static final String TAG = ACTIVITY_SERVICE;
    private Button btnLogin;
    private TextView tv_register;
    private EditText et_phon, etPassword;
    private String userName = "aaa", pass = "123";
    private CheckBox cbx_pass, cbx_log;
    private SharedPreferences spfRecorid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        getSupportActionBar().setTitle("登录");
        spfRecorid = getSharedPreferences("spfRecorid", MODE_PRIVATE);

        initView();
        initData(spfRecorid);

        tv_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
//                startActivity(intent); 需要数据回传.如果注册页面成功了,登录页面就结束了;;

                startActivityForResult(intent, RegisterActivity.RESULT_CODE_REGISTER);
            }

        });

        btnLogin.setOnClickListener(new View.OnClickListener() {
            @SuppressLint("CommitPrefEdits")
            @Override
            public void onClick(View view) {
                String phon = et_phon.getText().toString();
                String passWord = etPassword.getText().toString();

                if (TextUtils.isEmpty(phon)) {
                    Toast.makeText(LoginActivity.this, "你还没有注册账号,请先注册账号", Toast.LENGTH_SHORT).show();
                    return;
                }


                if (TextUtils.equals(phon, userName)) {
                    if (TextUtils.equals(passWord, pass)) {
                        if (cbx_pass.isChecked()) {

                            SharedPreferences.Editor editor = spfRecorid.edit();
                            editor.putString("phone", phon);
                            editor.putString("passWord", passWord);
                            editor.putBoolean("isRemeber", true);
                            if (cbx_log.isChecked()) {
                                editor.putBoolean("isLogin", true);
                            } else {
                                editor.putBoolean("isLogin", false);
                            }
                            editor.apply();
                        } else {
                            SharedPreferences.Editor edit = spfRecorid.edit();
                            edit.putBoolean("isRemeber", false);
                            edit.apply();
                        }

                        startActivityTo(phon);

                        Toast.makeText(LoginActivity.this, "登录成功了", Toast.LENGTH_SHORT).show();

                    } else {
                        Toast.makeText(LoginActivity.this, "密码不正确", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(LoginActivity.this, "账号不正确", Toast.LENGTH_SHORT).show();
                }

            }
        });

        Log.d(TAG, "onCreate: -----" + userName.toString());

    }

    private void startActivityTo(String phon) {
        Intent intent = new Intent(LoginActivity.this, MainActivity.class);
        intent.putExtra("phone", phon);
        startActivity(intent);
        LoginActivity.this.finish();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 0 && resultCode == 0 && data != null) {
            Bundle extras = data.getExtras();
            String phone = extras.getString("phone");
            String passWord = extras.getString("passWord");

            userName = phone;
            pass = passWord;

            et_phon.setText(userName);
            etPassword.setText(passWord);
            cbx_pass.setChecked(true);

            Log.i("TAG", "LoginActivity onActivityResult()-----1111");
        }
        Log.i("TAG", "LoginActivity onActivityResult()----requestCode == " +
                requestCode + "resultCode ==" + resultCode);

    }

    private void initData(SharedPreferences spfRecorid) {
        String phone = spfRecorid.getString("phone", "");
        String passWord = spfRecorid.getString("passWord", "");
        boolean isLogin = spfRecorid.getBoolean("isLogin", false);
        boolean isRemeber = spfRecorid.getBoolean("isRemeber", false);
        userName = phone;
        pass = passWord;
        if (isLogin) {
            startActivityTo(phone);
        }
        if (isRemeber) {
            et_phon.setText(phone);
            etPassword.setText(passWord);
            cbx_pass.setChecked(isRemeber);
        }

    }

    // 加载布局文件
    private void initView() {
        btnLogin = findViewById(R.id.btnLogin);
        et_phon = findViewById(R.id.et_phon);
        etPassword = findViewById(R.id.etPassword);
        cbx_pass = findViewById(R.id.cbx_pass);
        cbx_log = findViewById(R.id.cbx_log);
        tv_register = findViewById(R.id.tv_register);

        // 设置自动登录的监听
        cbx_log.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b) {
                    cbx_pass.setChecked(true);
                }

            }
        });

        cbx_pass.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (!b) {
                    cbx_log.setChecked(false);
                }
            }
        });

    }
}